package com.icss.th.subjects.facade.mango;

import java.util.ArrayList;
import java.util.List;

import com.icss.th.common.config.SystemConfig;
import com.icss.th.db.MongoConnection;
import com.icss.th.subjects.facade.ISubjectsFacade;
import com.icss.th.subjects.pojo.Subject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class SubjectsFacadeMongo implements ISubjectsFacade 
{
	@Override
	public List<Subject> getChildren(Subject subject) 
	{
		if (subject == null) return null;
		
		DBCollection collection = MongoConnection.getCollection("subjects");
		if (collection == null) return null;
			
		DBObject query = new BasicDBObject("parentID", subject.getId())
		.append("examIdCount", new BasicDBObject("$gte", Integer.valueOf(SystemConfig.SUBJECT_EXAM_COUNT)));
		DBObject fields = new BasicDBObject("sTitle", true).append("depLv", true);
		DBCursor cursor = collection.find(query, fields);
		
		List<Subject> list = new ArrayList<Subject>();
		try
		{
			while (cursor.hasNext())
			{
				DBObject obj = cursor.next();
				int id = (Integer)obj.get("_id");
				String name = (String)obj.get("sTitle");
				int level = (Integer)obj.get("depLv");
				list.add(new Subject(id, name, level, subject));
			}
		}
		finally
		{
			cursor.close();
		}
		
		return list;
	}

	@Override
	public List<Subject> getPopular(int topNum) 
	{
		// TODO Auto-generated method stub
		return null;
	}
}
